package com.jxc.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class UtilDate {
	/**
	 * function:将字符串类型的参数转换为日期类型，返回日期是：2010-01-15 00:00:00
	 * 
	 * @param sdate
	 *            例如："2010-01-15"
	 * @return
	 */
	public static Date strToDate(String sdate) {
		Date d = new Date();
		if (sdate==null || sdate.equals("")) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			d = sdf.parse(sdate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * function:将字符串类型的参数转换为日期+时间类型，返回时间是：2010-01-15 13:12:22
	 * 
	 * @param stime
	 *            例如："2010-01-15 13:12:22"
	 * @return
	 */
	public static Date strToTime(String stime) {
		Date d = new Date();
		if (stime==null || stime.equals("")) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			d = sdf.parse(stime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * function:将日期类型的参数转换为字符串类型，格式是：2010-01-15
	 * 
	 * @param sdate
	 * @return
	 */
	public static String dateToString(Date date) {
		String sdate = "";
		if (date == null) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sdate = sdf.format(date);
		return sdate;
	}

	/**
	 * function:将日期＋时间类型的参数转换为字符串类型，格式是：2010-01-15 13:12:22
	 * 
	 * @param sdate
	 * @return
	 */
	public static String timeToString(Date date) {
		String sdate = "";
		if (date == null) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		sdate = sdf.format(date);
		return sdate;
	}

	/**
	 * 
	 * @param dateA
	 *            开始时间
	 * @param dateB
	 *            结束时间
	 * @return
	 * @throws ParseException
	 */
	public static String getBetweenDay(String dateA, String dateB)
			throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.util.Date now = df.parse(dateB);
		java.util.Date date = df.parse(dateA);
		long l = now.getTime() - date.getTime();
		long day = l / (24 * 60 * 60 * 1000);
		long hour = (l / (60 * 60 * 1000) - day * 24);
		long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
		long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		String sda = "" + day + "天" + hour + "小时" + min + "分" + s + "秒";
		System.out.println(sda);
		return sda;
	}

	/**
	 * 
	 * @param dateA
	 *            开始时间
	 * @param dateB
	 *            结束时间
	 * @return
	 * @throws ParseException
	 */
	public static long gethour(Date dateA, Date dateB)
			throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.util.Date now = df.parse(timeToString(dateB));
		java.util.Date date = df.parse(timeToString(dateA));
		long l = now.getTime() - date.getTime();
		long day = l / (24 * 60 * 60 * 1000);
		long hour = (l / (60 * 60 * 1000) - day * 24);
		long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
		long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		if (min > 0) {
			return (hour + 1);
		} else {
			return hour;
		}
	}

	/**
	 * function:计算年龄
	 * 
	 * @param sdate
	 * @return
	 */
	public static int getAge(Date birthDay) {
		Calendar cal = Calendar.getInstance();
		// Date birthDay= UtilDate.strToDate(birth);
		if (cal.before(birthDay)) {
			throw new IllegalArgumentException(
					"The birthDay is before Now.It's unbelievable!");
		}
		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH);
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
		cal.setTime(birthDay);
		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH);
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
		int age = yearNow - yearBirth;
		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				// monthNow==monthBirth
				if (dayOfMonthNow < dayOfMonthBirth) {
					age--;
				} else {
					// do nothing
				}
			} else {
				// monthNow>monthBirth
				age--;
			}
		} else {
			// monthNow<monthBirth
			// donothing
		}

		return age;
	}

	/**
	 * function:将日期类型的参数转换为字符串类型，格式是：2010-01-15
	 * 
	 * @param sdate
	 * @return
	 */
	public static String dateToString(String date) {
		String sdate = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sdate = sdf.format(date);
		return sdate;
	}

	/**
	 * FUNCTION:将字符串转化为 ""
	 * 
	 * @param name
	 * @return
	 */
	public static String setNullToBanlk(String name) {
		if (name == null) {
			name = "";
		}
		return (name != null) ? name.trim() : "";
	}

	/**
	 * FUNCTION:将字符串转化为 ""
	 * 
	 * @param name
	 * @return
	 */
	public static String setNullToEmptyString(Object obj) {
		
		if (obj == null) {
			return "";
		}
		else
		{
			return obj.toString();
		}
	}
	
	/**
	 * FUNCTION:将字符串转化为 0
	 * 
	 * @param name
	 * @return
	 */
	public static String setNullToZero(Object name) {
		if (name == null)
			name = "0";
		if (name.equals(""))
			name = "0";
		return (name != null) ? name.toString().trim() : "0";
	}

	/*
	 * FUNCTION:将整型转化为0
	 * 
	 */
	public static Integer setNullToZero1(Integer name) {
		if (name == null)
			name = 0;
		;
		return (name != null) ? name : 0;
	}
	public static String allWhitereplace(Object obj) { // 过滤换行
		String s=obj==null?"":obj.toString();
//		String sd=(s.indexOf("\n") != -1)?s.replaceAll("\n", "\r\n"):s;
		return (s.indexOf("\n") != -1)?s.replaceAll("\n", "  "):s;//本想将\r\n，替换成 <br/>的，但textarea中<br/>没效果，所以暂时先将换行的改成 空串  
	}
	public static void main(String[] args) {
		/*
		 * String s = "2010-01-22"; Date d = strToDate(s);
		 * System.out.println(dateToString(d)); s = "2010-01-22 12:15:22"; d =
		 * strToTime(s); System.out.println(timeToString(d));
		 */
		System.out.println(UtilDate.strToDate("2010-01-01"));
		System.out.println(UtilDate.strToTime("2010-01-01 00:59:21"));
		System.out.println(UtilDate.dateToString(new Date()));
		System.out.println(UtilDate.timeToString(new Date()));
		/*
		 * System.out.println(timeToString(new Date(d.getTime())));
		 * java.sql.Date d1 = new java.sql.Date(d.getTime());
		 * System.out.println(timeToString(d1));
		 */
	}

}
